package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsSysParamMapper;
import cc.rengu.igas.cops.common.entity.CopsSysParam;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.dbs.Database;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 签约信息表(T_P_SIGN_ACCT_INFO) 操作接口实现类
 *
 * @author 王成
 * @date 2018/4/24.
 */
public class CopsSysParamMapperImpl implements CopsSysParamMapper {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);


    @Override
    public List<SysParam> getSysParamInfoList(String paramType, String paramKey) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsgetSysParamInfoList");
        SysParam sysParam = new SysParam();
        sysParam.setParamType(paramType);
        sysParam.setParamKey(paramKey);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, sysParam, SysParam.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据paramType:<{}>,paramKey:<{}>查询系统参数表信息失败", paramType, paramKey);
            return null;
        }
        return objectList.stream().map(item -> (SysParam) item).collect(Collectors.toList());
    }

    @Override
    public CopsSysParam getSysParamInfoBuyPrimaryKey(String instId, String paramType, String paramKey) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectSysparamByPrimaryKey");
        CopsSysParam copsSysParam = new CopsSysParam();
        copsSysParam.setInstId(instId);
        copsSysParam.setParamType(paramType);
        copsSysParam.setParamKey(paramKey);
        int recode = dbsUtil.dbsExecuteSql(sqlStr, copsSysParam, copsSysParam);

        if (Database.DBS_NOT_FOUND == recode) {
            rglog.info("系统参数信息查询无记录，instId:<{}>,paramType:<{}>,paramKey:<{}>", instId, paramType, paramKey);
            return null;
        } else if (Database.DBS_SUCCESS != recode) {
            rglog.info("系统参数信息查询无记录，instId:<{}>,paramType:<{}>,paramKey:<{}>", instId, paramType, paramKey);
            return null;
        }
        return copsSysParam;
    }
}
